<?php
include 'controlador.php';

$c = new Controller();
$con = $c->checkSession();
?>
<html>
    <head>
        <title>
            <?php
            echo $c->getTitle(0);
            ?>
        </title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link href="http://fonts.googleapis.com/css?family=Arvo" rel="stylesheet" type="text/css" />
        <link href="style.css" rel="stylesheet" type="text/css" media="screen" />
    </head>
    <!--<IMG border="0" align="middle" height="141" width="1350" src="images/head.jpg"> -->
    <body>

        <div id="wrapper">
            <div id="header">
                <div id="logo">			
                    <h1><a>
                            <?php
                            echo $c->getTitle(0);							
                            ?>
                        </a></h1>
                    <p><a> 
                            <?php
                            echo $c->getTitle(1);                            
                            ?>			 
                        </a></p>
                </div>

            </div>
            <!-- end #header -->
            <div id="menu">
                <ul>
                    <li class="current_page_item"><a href="<?php echo $c->f_action(0); ?>"><img src="images/salir.png"/><?php echo $c->getLink(0); ?></a></li>			
                </ul>
            </div>

            <!-- end #menu -->
            <div id="page">
                <div id="page-bgbtm">
                    <div id="content">

                        <div class="post">
                            <h2 class="title">Asignar docentes a cursos:</h2>   
                            <p class="meta"/>
                            <div class="entry">
                                <div class="entry-bgtop">
                                    <div class="entry-bgbtm">
                                        <?php
										
							
		if (isset($_POST['docente_id_docente'])) $docente = $_POST['docente_id_docente'];
                if (isset($_POST['ciclo_id_ciclo'])) $ciclo = $_POST['ciclo_id_ciclo'];
                if (isset($_POST['curso_id_curso'])) $curso = $_POST['curso_id_curso'];
                if (isset($_POST['horario_docente_id_horario_docente'])) $horariodocente = $_POST['horario_docente_id_horario_docente'];
                if (isset($_POST['salon_id_salon'])) $salon = $_POST['salon_id_salon'];
                if (isset($_POST['hora_inicio'])) $horainicio = $_POST['hora_inicio'];
                if (isset($_POST['hora_fin'])) $horafin = $_POST['hora_fin'];
                
                
							
		$ConnexionPDO = $con;
        $ConnexionPDO->query("SET NAMES 'utf8'");


        $Table = "docente_curso";
        $selectCol = "*";
		$WhereClause = "";
		$Clave_Primaria = "";
		$param = array(PARAM_DEL, PARAM_EDIT, PARAM_SELECT, PARAM_ADD, PARAM_TABLE);
		
		$Nom_Cols = array();
		$Type_Cols = array();
		$Cell_Property = array();
		$Col_Title = array();
		$Col_Type = array();
		$Field_Day = array();
		$Field_Hour = array();
		
		$visiblePK = false;
		$posPK = 0;
                $nombre_ciclo = "";
//insercion
if (isset($_POST["insertar"]) && $_POST["insertar"] == "Ingresar") {        
		$col = "";

		$val = "";
		
        foreach ($_POST as $key => $value) {
            if ($key != "insertar") {
                $col = $col == "" ? "`" . $key . "`" : $col . ", `" . $key . "`";
                $val = $val == "" ? "'" . $value . "'" : $val . ", '" . $value . "'";
            }
        }
        $req = "INSERT INTO " . $Table . " (" . $col . ") VALUES (" . $val . ");";
        //echo "INSERT INTO ".$Table." (".$col.") VALUES (".$val.");";
        $ConnexionPDO->exec($req); //or die(print_r( $this->ConnexionPDO->errorInfo()))    
		unset($_POST["insertar"]);
    }
//fin insercion

		$detailName["ciclo_id_ciclo"] = array("id_ciclo", "nombre_ciclo", "ciclo", "ciclo"); 
		$detailName["curso_id_curso"] = array("id_curso", "nombre", "curso", "curso");
		$detailName["docente_id_docente"] = array("id_docente", "concat(nombre,' ',apellido)", "docente", "docente");
		$detailName["salon_id_salon"] = array("id_salon", "concat(numero,' ',(select e.nombre from edificio e where edificio_id_edificio=e.id_edificio))", "salon", "salon"); 
		$detailName["horario_docente_id_horario_docente"] = array("id_horario_docente", "concat(                hora_inicio, ' - ', hora_fin, ' : ', 
															if(lun is null,' ', ' Lun '), 
															if(mar is null,' ', ' Mar '), 
															if(mie is null,' ', ' Mie '), 
															if(jue is null,' ', ' Jue '),
															if(vie is null,' ', ' Vie '),
															if(sab is null,' ', ' Sab '),
															if(dom is null,' ', ' Dom ')
															) as horario", "horario_docente where plaza_id_plaza in (select id_plaza from plaza where docente_id_docente=".$docente.")", "horario"); 															
		$Field_Hour[6] = true;
		$Field_Hour[7] = true;
		
        $req = "select ".$selectCol." from " . $Table . " ". $WhereClause . " limit 0,1";

        $resultat = $ConnexionPDO->query($req);
        $Nb_Col = $resultat->columnCount();
		
        for ($i = 0; $i < $Nb_Col; $i++) {
            $Name_Col = $resultat->getColumnMeta($i); 

            //Encontrar la clave primaria 
            if (isset($Name_Col['flags'][1]) && $Name_Col['flags'][1] == "primary_key") {
                $Clave_Primaria = $Name_Col['name'];
                $posPK = $i;  //setea posicion de la PK
            }
            //Obtener los nombres de las columnas   
            $Nom_Cols[] = $Name_Col['name'];
            $Col_Title[$i] = strtoupper(str_replace("_", " ", $Name_Col['name']));      
            
            //Obtener los tipos de las columnas   
            $Type_Cols[] = $Name_Col['native_type'];
        };
		$Col_Title[1] = "Ciclo";
		$Col_Title[2] = "Curso";
		$Col_Title[3] = "Docente";
		$Col_Title[4] = "Horarios disponibles";
		$Col_Title[5] = "Salon";
		$link = "";
        foreach ($_GET as $key => $value) {
            if (!in_array($key, $param)) {
                $link = $link == "" ? "" : $link . "&";
                $link = $link . $key . "=" . $value;
            }
        }		
        $link = $link == "" ? "" : "?" . $link;
						
		echo "<form name='form_add' method='post' action='" . $_SERVER['PHP_SELF'] . $link . "'><table>";
        $i = 0;
        foreach ($Nom_Cols as $col) {
            if (!$visiblePK && $Clave_Primaria == $col) {
            }else if ($detailName[$col][0]) {  //$this->detailName[$col][0] //$col == 'puesto_id_puesto'
                
                $req = "select ".$detailName[$col][0].",".$detailName[$col][1]." from ".$detailName[$col][2] . " order by 2"; //$this->detailName[$col][0]//$req = "select id_puesto, nombre from puesto";
                //echo $req;
                $resultat = $ConnexionPDO->query($req);                
                echo "<tr>";
                echo "<td>".strtoupper(str_replace("_", " ", $Col_Title[$i]))."</td>";
                echo "<td>:</td>";             
                if($col == 'docente_id_docente')
					echo "<td><SELECT name='" . $col . "' onChange = 'document.form_add.submit()' >";
                else if($col == 'ciclo_id_ciclo'){
                    echo "<td><SELECT name='" . $col . "' onChange = 'document.form_add.submit()' >";
                }
					
                else 
					echo "<td><SELECT name='" . $col . "'>";
				$cont = 0;
                foreach ($resultat as $row){
					if(($col == 'docente_id_docente') && ($row[0] == $docente))
						echo '<option value="' . $row[0] . '" selected="selected">' . $row[1] . '</option>';
                                        else if(($col == 'ciclo_id_ciclo') && ($row[0] == $ciclo)){
                                            echo '<option value="' . $row[0] . '" selected="selected">' . $row[1] . '</option>';
                                            $nombre_ciclo = $row[1];
                                        }						
                                        else if(($col == 'curso_id_curso') && ($row[0] == $curso))
						echo '<option value="' . $row[0] . '" selected="selected">' . $row[1] . '</option>';
                                        else if(($col == 'horario_docente_id_horario_docente') && ($row[0] == $horariodocente))
						echo '<option value="' . $row[0] . '" selected="selected">' . $row[1] . '</option>';
                                        else if(($col == 'salon_id_salon') && ($row[0] == $salon))
						echo '<option value="' . $row[0] . '" selected="selected">' . $row[1] . '</option>';                                                                                
					else if(($col == 'docente_id_docente')&&($cont == 0)){
						echo '<option value="-1" selected="selected"></option>';
						echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
						}
                                        else if(($col == 'ciclo_id_ciclo')&&($cont == 0)){
						echo '<option value="-1" selected="selected"></option>';
						echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
						}
					else
						echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
				$cont++;
                }                 				
                echo "</SELECT></td>";
                echo "</tr>";
                
            }else if($Field_Hour[$i]){ //es campo de hora
                echo "<tr>";
                echo "<td>".$Col_Title[$i]."</td>";
                echo "<td>:</td>";
                echo "<td><SELECT name='" . $col . "'>";                    
                for ($hr = 7; $hr <= 21; $hr++) {                     
                    echo "<option value='" . $hr . ":00:00'>" . $hr . ":00 hrs.</option>";
                }                
                echo "</SELECT></td>";
                echo "</tr>";                                
            }else{                
                echo "<tr>";
                echo "<td>" . $Col_Title[$i] . "</td>"; //$this->TextFormat($col)
                echo "<td>:</td>";
                echo "<td><input type='text' name='" . $col . "' '". $Col_Type[$i] . " /></td>";
                echo "</tr>";
            }
            $i++;
        }
        echo "<td></td>";
        echo "<td><input type='hidden' name='' value='add' /></td>";
        echo "<td><input type='submit' name= 'insertar' value='Ingresar'/> </td>";
        echo "</table></form>";
                                        ?>										
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="post">                            
                            <h2 class="title">Docentes asignados <?php if($nombre_ciclo)echo " en el ciclo: ". $nombre_ciclo; ?></h2> 
                            <p class="meta"/>
                            <div class="entry">
                                <div class="entry-bgtop">
                                    <div class="entry-bgbtm">
                                        <?php
                                        
                                                                                
										require_once 'DataGrid_1.php';

                                        //$grid = new DataGrid_1("docente_curso", 'id_docente_curso,ciclo_id_ciclo,curso_id_curso,docente_id_docente', $con);
										$grid = new DataGrid_1("docente_curso", 'id_docente_curso,curso_id_curso,docente_id_docente, horario_docente_id_horario_docente, salon_id_salon,hora_inicio, hora_fin', $con);
										if($ciclo) $grid->SetWhereClause("where ciclo_id_ciclo = ".$ciclo);
                                                                                $grid->SetOrderBy("order by curso_id_curso, docente_id_docente");
                                                                                $grid->SetDetailName("ciclo_id_ciclo", "id_ciclo", "nombre_ciclo", "ciclo", "ciclo");
                                        $grid->SetDetailName("curso_id_curso", "id_curso", "nombre", "curso", "curso");
                                        $grid->SetDetailName("docente_id_docente", "id_docente", "concat(nombre,' ',apellido)", "docente", "docente");
										$grid->SetDetailName("salon_id_salon", "id_salon", "concat(numero,' ',(select e.nombre from edificio e where edificio_id_edificio=e.id_edificio))", "salon", "salon");
										$grid->SetDetailName("horario_docente_id_horario_docente", "id_horario_docente", 
                                                "concat(
															if(lun is null,' ', ' L '), 
															if(mar is null,' ', ' M '), 
															if(mie is null,' ', ' Mi '), 
															if(jue is null,' ', ' J '),
															if(vie is null,' ', ' V '),
															if(sab is null,' ', ' S '),
															if(dom is null,' ', ' D')
															)",
                                                "horario_docente", "horario");                                        
                                        //$grid->SetModeAdd(true);
                                        //$grid->SetModeEdit(true);
                                        $grid->SetModeDelete(true);										
                                        //$grid->SetNameColumn(1, "Ciclo");
										$grid->SetNameColumn(1, "Curso");
										$grid->SetNameColumn(2, "Docente");
										$grid->SetNameColumn(3, "Horario");
										$grid->SetNameColumn(4, "Salon");
										$grid->SetHourField(5, true);
                                        $grid->SetHourField(6, true);
                                                                               
                                        $grid->Show();
										
										?>
                                    </div>
                                </div>
                            </div>
                        </div>                        

                        <div style="clear: both;">&nbsp;</div>
                    </div>
                    <!-- end #content -->
                    <div id="sidebar">
                        <?php echo $c->getOpciones(); ?>
                    </div>
                    <!-- end #sidebar -->
                    <div style="clear: both;">&nbsp;</div>
                </div>
            </div>
            <!-- end #page -->
        </div>	
        <div id="footer">
            <p><?php echo $c->getTexto(7); ?></p>
        </div>
    </body>
</html>
